<?php
class MainAction extends FrontAction {
    private $db = FALSE;

    protected function init() {
        if(!$this->checkConsoleLogin()) {
            $this->redirect('member/login');
            exit;
        }
        $this->assign('user',$this->user);
        $this->assign('user_info',$this->user_info);
        $this->assign('root_path',$this->root_path);

    }
    public function index() {
        $this->db = M('article');
        cookie('request_url',$this->getCurrentUrl());
        $cid = abs($_GET['cid']);
        $where = ($cid) ? 'cid = '.$cid : '';
        $data = $this->getPageData($this->db,6,$where,'`update_time` DESC');
        $cate = D('Cate')->getCate();
      
        $this->assign('data',$data);
        $this->assign('cate',$cate);
        $this->display();
    }
    public function articleAdd() {
        $cate = D('Cate')->getCate();
        $this->assign('cate',$cate);
        $this->display();
    }
    public function articleAddPost(){
        if (!$this->validTR(true)){
            alert('参数非法','','2');
            exit;
        }
        $data['mid'] = $this->user['mid'];
        $tmp_cate = explode(',',$_POST['cate']);
        $data['cid'] = abs($tmp_cate[0]);
        $data['cname'] = (strpos($tmp_cate[1],'|') !== false ) ? substr($tmp_cate[1],13) : $tmp_cate[1];
        $data['title'] = h($_POST['title']);
        $data['author'] = (strpos($_POST['author'],',') === false) ? $_POST['author'] : serialize(explode($_POST['author'], ','));
        $data['keywords'] = h($_POST['keywords']);
        $data['abstract'] = $_POST['abstract'];
        $data['content'] = $_POST['content'];
        $data['add_time'] = $data['update_time'] = $this->timestamp;
        $data['is_display'] = abs($_POST['is_display']);
        if ($data['cid']===0){
            alert('请选择分类','','2');
            exit;
        }
        if (empty($data['title'])){
            alert('标题不能为空','','2');
            exit;
        }
        $aid = M('article')->data($data)->add();
        $att_id = cookie('att_id');
        if($aid && !empty($att_id)){
            M('att')->data(array('aid'=>$aid))->where('att_id IN ('.$att_id.')')->save();
            cookie('att_id',NULL);
        }
        alert('添加成功','main/index');
        exit;
    }
    public function articleEdit(){
        $aid = abs($_GET['aid']);
        $data = M('article')->find($aid);
        $cate = D('Cate')->getCate();
        $att = M('att')->where(array('aid'=>$aid))->findAll();
        $this->assign('cate',$cate);
        $this->assign('data',$data);
        $this->assign('att',$att);
        $this->display();
    }
    public function articleEditPost(){
        $data['aid'] = abs($_POST['aid']);
        if (!$this->validTR(true) || $data['aid'] === 0){
            alert('参数非法','','2');
            exit;
        }
        $data['mid'] = $this->user['mid'];
        $tmp_cate = explode(',',$_POST['cate']);
        $data['cid'] = abs($tmp_cate[0]);
        $data['cname'] = (strpos($tmp_cate[1],'|') !== false ) ? substr($tmp_cate[1],13) : $tmp_cate[1];
        $data['title'] = h($_POST['title']);
        $data['author'] = (strpos($_POST['author'],',') === false) ? $_POST['author'] : serialize(explode($_POST['author'], ','));
        $data['keywords'] = h($_POST['keywords']);
        $data['abstract'] = $_POST['abstract'];
        $data['content'] = $_POST['content'];
        $data['update_time'] = $this->timestamp;
        $data['is_display'] = abs($_POST['is_display']);
        if ($data['cid']===0){
            alert('请选择分类','','2');
            exit;
        }
        if (empty($data['title'])){
            alert('标题不能为空','','2');
            exit;
        }
        M('article')->data($data)->save();
        alert('修改成功','main/index');
        exit;
    }
    public function articleDel(){
        (!$this->validReferer(true)) && exit('2');
        if ($this->ajax){
            $aid = $_POST[aid];
            ($aid === 0) && exit('2');
            $att = M('att')->where(array('aid'=>$aid))->findAll();
            if($att){
                foreach((array)$att as $value){
                    $path = $this->root_rel_path.'/u/'.$value['path'];
                    @unlink($path);
                }
                M('att')->where(array('aid'=>$aid))->delete();
            }
            M('article')->delete($aid);
            exit('1');
        }
        exit('2');
    }
    public function attDel(){
        (!$this->validReferer(true)) && exit('2');
        if ($this->ajax){
            $att_id = abs($_POST['att_id']);
            $att = M('att')->find($att_id);
            $path = $this->root_rel_path.'/u/'.$att['path'];
            if(file_exists($path)){
                if(@unlink($path)){
                    M('att')->delete($att_id);
                    exit('1');
                }
            }
        }
    }
    public function upload(){
        if($this->post){
            import("ORG.Net.UploadFile");
            $upload = new UploadFile(5097152,'txt,rar,zip,jpg,jpeg,gif,png,swf,wmv,avi,wma,mp3,mid,',array(),'./u/','uniqid');
            $upload->autoSub = true;
            $upload->subType = 'date';
            $upload_info = $upload->uploadOne('filedata');
            if($upload_info){
                $msg='!'.$this->root_path.'u/'.$upload_info[0]['savename'];
                $data = array('path'=>$upload_info[0]['savename']);
                $aid = M('att')->data($data)->add();
                $att_id = (cookie('att_id')) ? cookie('att_id').','.$aid : $aid;
                cookie('att_id',$att_id);
            }
            $re = json_encode(array('err'=>$upload->getErrorMsg(),'msg'=>$msg));
            echo $re;
        }
        exit;
    }
    public function googlemap(){
        $this->display();
    }
    public function cate(){
        $data = D('Cate')->getCate();
        $this->assign('data',$data);
        $this->display();
    }
    public function cateAdd(){
        if($this->post){
            if(!$this->validTR()){
                alert('非法','main/cateAdd');
                exit;
            }
            if(!$_POST['index_display'] || !$_POST['single_page']) {
                alert('非法','main/cateAdd');
                exit;
            }
            $this->db = M('cate');
            /*if($this->db->where(array('cname_hash'=>md5($_POST['cname'])))->findAll()){
                 alert('分类名不能重复','main/cateAdd','1');
                 exit;
            }*/
            $data['fcid'] = intval($_POST['fcid']);
            if($_POST['cname'] ==''){
                alert('分类名不能为空','main/cateAdd','1');
                exit;
            }
            $data['cname'] = h($_POST['cname']);
            $data['cname_hash'] = md5($data['cname']);
            $data['index_display'] = abs($_POST['index_display']);
            $data['order'] = abs($_POST['order']);
            $data['single_page'] = abs($_POST['single_page']);
            if($this->db->data($data)->add()){
                $index_nav = M('cate')->where('index_display = 1')->order('`order` ASC')->findAll();
                $fcate = M('cate')->where(array('fcid'=>0))->findAll();
                F('fcache',$fcate,$this->cate_cache_path);
                F('nav_cate',$index_nav,$this->cate_cache_path);
                alert('添加成功','main/cate');
            }
            exit;
        }
        $fcate = F('fcache','',$this->cate_cache_path);
        if(!$fcate) {
            $fcate = M('cate')->where(array('fcid'=>0))->findAll();
            F('fcache',$fcate,$this->cate_cache_path);
        }
        $this->assign('data',$fcate);
        $this->display();
    }
    public function cateEdit() {
        if($this->post){
            if(!$this->validTR()){
                alert('非法','',2);
                exit;
            }
            if(!$_POST['index_display'] || !$_POST['single_page']) {
                alert('非法','main/cateAdd');
                exit;
            }
            $this->db = M('cate');
            /*if($this->db->where(array('cname_hash'=>md5($_POST['cname'])))->findAll()){
                alert('分类名不能重复','',2);
                exit;
            }*/
            $data['fcid'] = abs($_POST['fcid']);
            $data['cname'] = h($_POST['cname']);
            $data['cname_hash'] = md5($data['cname']);
            $data['index_display'] = abs($_POST['index_display']);
            $data['order'] = abs($_POST['order']);
            $data['single_page'] = abs($_POST['single_page']);
            $cid = abs($_POST['cid']);
            if($this->db->data($data)->where(array('cid'=>$cid))->save()) {
                M('article')->data(array('cname'=>$data['cname']))->where(array('cid'=>$cid))->save();
                $index_nav = M('cate')->where('index_display = 1')->order('`order` ASC')->findAll();
                $fcate = M('cate')->where(array('fcid'=>0))->findAll();
                F('fcache',$fcate,$this->cate_cache_path);
                F('nav_cate',$index_nav,$this->cate_cache_path);
                alert('修改成功','main/cate');
            }
            exit;
        }
        $cid = abs($_GET['cid']);
        $fcate = F('fcache','',$this->cate_cache_path);
        if(!$fcate) {
            $fcate = M('cate')->where(array('fcid'=>0))->findAll();
            F('fcache',$fcate,$this->cate_cache_path);
        }
        $cate_curr = M('cate')->where(array('cid'=>$cid))->find();
        $fcate_curr[$cate_curr['fcid']]='selected="selected"';
        $this->assign('cate_curr',$cate_curr);
        $this->assign('fcate',$fcate);
        $this->assign('fcate_curr',$fcate_curr);
        $this->display();
    }

    public function cateOrder(){
        if($this->ajax){
            $order = json_decode(str_replace('\\','',$_POST['order']));
            $order == '' && exit ($order);
            foreach((array)$order as $value){
                foreach($value as $key=>$val){
                    M('cate')->where(array('cid'=>$key))->data(array('order'=>$val))->save();
                }
            }
            $index_nav = M('cate')->where('index_display = 1')->order('`order` ASC')->findAll();
            $fcate = M('cate')->where(array('fcid'=>0))->findAll();
            F('fcache',$fcate,$this->cate_cache_path);
            F('nav_cate',$index_nav,$this->cate_cache_path);
        }
    }
}